//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package objects;

import location.DisplayProductionObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

public class FruitsProduction extends ProductionFactories {
    private final String treeName;
    private List<DisplayProductionObject> readyToRemove = new ArrayList();

    public FruitsProduction(String tree) {
        this.treeName = tree;
    }

    public String getProductionName() {
        return this.treeName;
    }

    public int getObjectClass() {
        return 61;
    }

    public boolean checkUnlockLevel(ProductionsGoodEntry pge, Integer level) {
        return false;
    }

    public int getUnitsMultiplier() {
        return 1;
    }

    public void updateTicksLeft(int ticksNow) {
        Iterator var3 = this.productions.values().iterator();

        while(var3.hasNext()) {
            DisplayProductionObject dpo = (DisplayProductionObject)var3.next();
            dpo.updateTicksLeftAll(ticksNow);
        }

    }

    public void clear() {
        this.productions.clear();
        this.readyToRemove.clear();
    }

    public ProductionsGoodEntry getGoodById(Integer id) {
        FruitTreeEntry fe = FruitTrees.getByName(this.getProductionName());
        return fe == null?null:fe.getGoodById(id);
    }

    public ProductionsGoodEntry getGoodByName(String name) {
        FruitTreeEntry fe = FruitTrees.getByName(this.getProductionName());
        return fe == null?null:fe.getGoodByName(name);
    }

    public Collection<ProductionsGoodEntry> getGoods() {
        ArrayList rval = new ArrayList();
        FruitTreeEntry fe = FruitTrees.getByName(this.getProductionName());
        if(fe != null) {
            rval.add(fe.getGoodByName(fe.getFruit()));
        }

        return rval;
    }

    public int initialSize() {
        return this.productions.size() + this.readyToRemove.size();
    }

    public List<DisplayProductionObject> claimRemovedTrees(Integer coins, LinkedHashMap<Integer, Integer> inventory, Integer maxClaimCount) {
        ArrayList val = new ArrayList();
        Integer price = Integer.valueOf(0);
        Integer count = Integer.valueOf(0);
        Iterator var8 = this.readyToRemove.iterator();

        DisplayProductionObject rm;
        while(var8.hasNext()) {
            rm = (DisplayProductionObject)var8.next();
            FruitTreeEntry fte = FruitTrees.getById(Integer.valueOf(rm.id()));
            ToolsEntry tool = Tools.getByName(fte.getTool());
            Integer toolCount = (Integer)inventory.get(tool.getGlobalID());
            if(count.intValue() >= maxClaimCount.intValue() || toolCount == null || toolCount.intValue() < 1 || price.intValue() + fte.getPrice().intValue() >= coins.intValue()) {
                break;
            }

            price = Integer.valueOf(price.intValue() + fte.getPrice().intValue());
            val.add(rm);
            count = Integer.valueOf(count.intValue() + 1);
            inventory.put(tool.getGlobalID(), Integer.valueOf(toolCount.intValue() - 1));
        }

        var8 = val.iterator();

        while(var8.hasNext()) {
            rm = (DisplayProductionObject)var8.next();
            this.readyToRemove.remove(rm);
        }

        return val;
    }

    public LinkedHashMap<Integer, List<claimedFruits>> claimReadyFruits(int maxClimeCount) {
        LinkedHashMap ready = new LinkedHashMap();
        int climedCount = 0;
        Iterator var5 = this.productions.values().iterator();

        while(var5.hasNext()) {
            DisplayProductionObject dpo = (DisplayProductionObject)var5.next();
            if(dpo.getReadySlotsCount() > 0) {
                ArrayList fruits = new ArrayList();
                ArrayList indexes = new ArrayList();
                dpo.climeReady(maxClimeCount - climedCount, fruits, indexes);
                climedCount += fruits.size();
                if(fruits.size() > 0) {
                    ArrayList l = new ArrayList();

                    for(int i = 0; i < fruits.size(); ++i) {
                        if(fruits.size() != indexes.size()) {
                            System.out.println("fruits.size() != indexes.size()");
                        }

                        l.add(new claimedFruits((Integer)fruits.get(i), (Integer)indexes.get(i)));
                    }

                    ready.put(Integer.valueOf(dpo.getProductionInstanceGlobalID()), l);
                }
            }

            if(climedCount == maxClimeCount) {
                break;
            }
        }

        return ready;
    }

    public DisplayProductionObject getDisplayObject(Integer globalInstanceId) {
        return (DisplayProductionObject)this.productions.get(globalInstanceId);
    }

    public void readyToRemove(DisplayProductionObject dpo) {
        DisplayProductionObject removed = (DisplayProductionObject)this.productions.remove(Integer.valueOf(dpo.getProductionInstanceGlobalID()));
        if(removed != null) {
            this.readyToRemove.add(removed);
        }

    }

    public void addRedyToRemove(DisplayProductionObject obj) {
        this.readyToRemove.add(obj);
    }

    LinkedHashMap<String, Integer> toolsNeeded() {
        LinkedHashMap val = new LinkedHashMap();
        String toolName = FruitTrees.getByName(this.treeName).getTool();
        Integer count = Integer.valueOf(this.productions.size() + this.readyToRemove.size());
        val.put(toolName, count);
        return val;
    }
}
